Component({
  /**
   * 组件的属性列表
   */
  properties: {
    carNum: { // 选择器 选项数组中 对象的value的默认key
      type: String,
      value: ''
    },
  },
  observers: {
    'carNum': function (val) {
      let inputPlates = {
        index0: val[0],
        index1: val[1],
        index2: val[2],
        index3: val[3],
        index4: val[4],
        index5: val[5],
        index6: val[6],
        index7: val[7],
      }

      this.setData({
        inputPlates: inputPlates
      })


    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    isLoading: false,
    appUserId: "",
    escapeOrderList: [],
    carNumList: [],
    hasOrder: false,
    isKeyboard: false,
    isNumberKB: true,
    tapNum: false,
    disableKey: "1234567890港澳学",
    // keyboardNumber: "1234567890ABCDEFGHJKLMNPQRSTUVWXYZ港澳学",
    keyboardNumber: "1234567890QWERTYUP港澳ASDFGHJKL学ZXCVBNM",
    keyboard1: "渝川京沪粤津冀晋蒙辽吉黑苏浙皖闽赣鲁豫鄂湘桂琼贵云藏陕甘青宁新",
    inputPlates: {
      index0: "",
      index1: "",
      index2: "",
      index3: "",
      index4: "",
      index5: "",
      index6: "",
      index7: ""
    },
    inputOnFocusIndex: "",
    isNewEnergy: false,
  },

  /**
   * 组件的方法列表
   */
  lifetimes: {
    attached: function () {
      let car = wx.getStorageSync('vehicle_num');
      if (car) {
        let arr = {
          index0: car[0],
          index1: car[1],
          index2: car[2],
          index3: car[3],
          index4: car[4],
          index5: car[5],
          index6: car[6],
          index7: car[7],
        }
        this.setData({
          inputPlates: arr
        })

      } else {
        // console.log("!11");
      }
      console.log(car);

    },
    detached: function () {
      // 在组件实例被从页面节点树移除时执行
    },
  },
  methods: {
    //切换车牌
    changeplate: function () {
      var that = this;
      // console.log("isNewEnergy",that.data.isNewEnergy);
      that.setData({
        isNewEnergy: !that.data.isNewEnergy,
      });
      this.checkCarNum();
    },
    //切换车牌
    changeplate1: function () {
      var that = this;
      that.setData({
        flag: true,
        inputPlates: {
          index0: "",
          index1: "",
          index2: "",
          index3: "",
          index4: "",
          index5: "",
          index6: "",
          index7: ""
        },
      })
    },
    inputClick: function (t) {
      var that = this;
      //console.log('输入框:', t);
      // console.log('输入框:', t.target.dataset.id);

      if (t.target.dataset.id == 0) {
        that.setData({
          inputOnFocusIndex: t.target.dataset.id,
          isNumberKB: true,
          isKeyboard: true,
          tapNum: false,
        })
      };
      if (t.target.dataset.id == 1) {
        that.setData({
          inputOnFocusIndex: t.target.dataset.id,
          isNumberKB: false,
          isKeyboard: true,
          tapNum: false,
        })
      }
      if (t.target.dataset.id > 1) {
        that.setData({
          inputOnFocusIndex: t.target.dataset.id,
          isNumberKB: false,
          isKeyboard: true,
          tapNum: true,
        })
      }
    },
    //键盘点击事件
    tapKeyboard: function (t) {
      t.target.dataset.index;
      var a = t.target.dataset.val;
      //console.log("data",this.data);
      // console.log('键盘:',a);
      //console.log("index",t.target.dataset.index);
      //console.log("focus",this.data.inputOnFocusIndex);
      switch (parseInt(this.data.inputOnFocusIndex)) {
        case 0:
          this.setData({
            "inputPlates.index0": a,
            inputOnFocusIndex: "1"
          });
          break;
        case 1:
          this.setData({
            "inputPlates.index1": a,
            inputOnFocusIndex: "2"
          });
          break;
        case 2:
          this.setData({
            "inputPlates.index2": a,
            inputOnFocusIndex: "3"
          });
          break;
        case 3:
          this.setData({
            "inputPlates.index3": a,
            inputOnFocusIndex: "4"
          });
          break;
        case 4:
          this.setData({
            "inputPlates.index4": a,
            inputOnFocusIndex: "5"
          });
          break;
        case 5:
          this.setData({
            "inputPlates.index5": a,
            inputOnFocusIndex: "6"
          });
          break;
        case 6:
          this.setData({
            "inputPlates.index6": a,
            inputOnFocusIndex: "7"
          });
          break;
        case 7:
          this.setData({
            "inputPlates.index7": a,
            inputOnFocusIndex: "7"
          });
      }
      var n = this.data.inputPlates.index0 + this.data.inputPlates.index1 + this.data.inputPlates.index2 + this.data.inputPlates.index3 + this.data.inputPlates.index4 + this.data.inputPlates.index5 +
        this.data.inputPlates.index6 + this.data.inputPlates.index7
      // console.log('车牌号1:', n);
      // this.triggerEvent('car_num', {sonParam: n})

      this.data.carNum = n;
      this.checkedSubmitButtonEnabled();
      this.checkCarNum();
      let isNewEnergy1 = this.data.isNewEnergy
      let inputPlates1 = JSON.parse(JSON.stringify(this.data.inputPlates))
      if (!isNewEnergy1 && inputPlates1.index6 != "") {
        this.setData({
          isKeyboard: false,
        })
      }
      if (isNewEnergy1 && inputPlates1.index7 != "") {
        this.setData({
          isKeyboard: false,
        })
      }


    },
    //键盘关闭按钮点击事件
    tapSpecBtn: function (t) {
      var a = this,
        e = t.target.dataset.index;
      if (0 == e) {
        switch (parseInt(this.data.inputOnFocusIndex)) {
          case 0:
            this.setData({
              "inputPlates.index0": "",
              inputOnFocusIndex: "0"
            });
            break;
          case 1:
            this.setData({
              "inputPlates.index1": "",
              inputOnFocusIndex: "0"
            });
            break;
          case 2:
            this.setData({
              "inputPlates.index2": "",
              inputOnFocusIndex: "1"
            });
            break;
          case 3:
            this.setData({
              "inputPlates.index3": "",
              inputOnFocusIndex: "2"
            });
            break;
          case 4:
            this.setData({
              "inputPlates.index4": "",
              inputOnFocusIndex: "3"
            });
            break;
          case 5:
            this.setData({
              "inputPlates.index5": "",
              inputOnFocusIndex: "4"
            });
            break;
          case 6:
            this.setData({
              "inputPlates.index6": "",
              inputOnFocusIndex: "5"
            });
            break;
          case 7:
            this.setData({
              "inputPlates.index7": "",
              inputOnFocusIndex: "6"
            });
        }
        this.checkedSubmitButtonEnabled();
      } else 1 == e && a.setData({
        isKeyboard: !1,
        isNumberKB: !1,
        inputOnFocusIndex: ""
      });
      var n = this.data.inputPlates.index0 + this.data.inputPlates.index1 + this.data.inputPlates.index2 + this.data.inputPlates.index3 + this.data.inputPlates.index4 + this.data.inputPlates.index5 +
        this.data.inputPlates.index6 + this.data.inputPlates.index7
      console.log('车牌号:', n);
      this.data.carNum = n.replace(/[a-z]/g, '');
      this.checkCarNum();
    },
    //键盘切换
    checkedKeyboard: function () {
      var t = this;
      //console.log("键盘切换", this.data.inputOnFocusIndex);
      if (this.data.inputOnFocusIndex == 0) {
        t.setData({
          tapNum: false,
          isNumberKB: true
        })
      }
      if (this.data.inputOnFocusIndex == 1) {
        t.setData({
          tapNum: false,
          isNumberKB: false
        })
      }
      if (this.data.inputOnFocusIndex > 1) {
        t.setData({
          tapNum: true,
          isNumberKB: false
        })
      }
    },
    checkedSubmitButtonEnabled: function () {
      this.checkedKeyboard();
      var t = !0;
      for (var a in this.data.inputPlates)
        if ("index7" != a && this.data.inputPlates[a].length < 1) {
          t = !1;
          break;
        }
    },
    //校验车牌号-车牌输入限制了正确格式只判断车牌位数
    checkCarNum: function () {
      if (this.data.isNewEnergy && this.data.carNum.length < 8) {
        let res = {
          carNum: this.data.carNum,
          isPlate: false
        }
        this.triggerEvent("setCarNum", res);
        return false
      }
      if (!this.data.isNewEnergy) {
        if (this.data.carNum.length < 7) {
          let res = {
            carNum: this.data.carNum,
            isPlate: false
          }
          this.triggerEvent("setCarNum", res);
          return false
        } else {
          var carNum = this.data.carNum.substr(0, 7);
          let res = {
            carNum: carNum,
            isPlate: true
          }
          this.triggerEvent("setCarNum", res);
          return true;
        }
      }
      let res = {
        carNum: this.data.carNum,
        isPlate: true
      }
      this.triggerEvent("setCarNum", res);
      return true;
    }

  }

})
